#include "ftpprofilexmlparser.hpp"
#include <iostream>

using namespace com_oocl_riley;

FtpProfileXmlParser::FtpProfileXmlParser(const string &fileName)
    :XmlParser(fileName),_content(nullptr)
{
    _content=new vector<PtrConfig>;
}
FtpProfileXmlParser::~FtpProfileXmlParser()
{
    for(vector<PtrConfig>::iterator it=_content->begin(); it!=_content->end(); ++it)
        delete(*it);
    delete(_content);
}
void *FtpProfileXmlParser::getContent()
{
    return static_cast<void *>(_content);
}
void FtpProfileXmlParser::tranverse(xmlDocPtr doc,xmlNodePtr cur)
{

    if(xmlStrcasecmp(cur->name,CAST_TO_XMLCHAR"FtpProfileList"))
    {
        xmlFreeDoc(doc);
        return;
    }
    xmlNodePtr p=cur->children;
    //ftp
    while(p!=NULL)
    {


        if(p->type==XML_ELEMENT_NODE)
        {

            const xmlChar *name=p->name;
            if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "ftp")==0)
            {
                Config *config=new Config;
                xmlNodePtr pp=p->children;
                while(pp!=NULL)
                {
                    if(pp->type==XML_ELEMENT_NODE)
                    {
                        name=pp->name;
                        if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "host")==0)
                        {
                            config->_ftpInfo->Host(CAST_TO_CHAR xmlNodeGetContent(pp));
                        }
                        else if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "port")==0)
                        {
                            config->_ftpInfo->Port(CAST_TO_CHAR xmlNodeGetContent(pp));
                        }
                        else if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "username")==0)
                        {
                            config->_ftpInfo->Usr(CAST_TO_CHAR xmlNodeGetContent(pp));
                        }
                        else if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "password")==0)
                        {
                            config->_ftpInfo->Pwd(CAST_TO_CHAR xmlNodeGetContent(pp));
                        }
                        else if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "profilelist")==0)
                        {
                            xmlNodePtr cp=pp->children;

                            while(cp!=NULL)
                            {
                                if(cp->type==XML_ELEMENT_NODE)
                                {
                                    name=cp->name;
                                    if(!xmlStrcasecmp(name,CAST_TO_XMLCHAR "profile"))
                                    {
                                        xmlNodePtr cpp=cp->children;
                                        Profile *profile=new Profile;
                                        while(cpp!=NULL)
                                        {
                                            if(cpp->type==XML_ELEMENT_NODE)
                                            {
                                                name=cpp->name;
                                                if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "warehouse")==0)
                                                {
                                                    profile->Warehouse(CAST_TO_CHAR xmlNodeGetContent(cpp));
                                                }
                                                else if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "owner")==0)
                                                {
                                                    profile->Owner(CAST_TO_CHAR xmlNodeGetContent(cpp));
                                                }
                                                else if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "profilename")==0)
                                                {
                                                    profile->ProfileName(CAST_TO_CHAR xmlNodeGetContent(cpp));
                                                }
                                                else if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "ordertype")==0)
                                                {
                                                    profile->OrderType(CAST_TO_CHAR xmlNodeGetContent(cpp));
                                                }
                                                else if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "datadirection")==0)
                                                {
                                                    profile->DataDiretion(CAST_TO_CHAR xmlNodeGetContent(cpp));
                                                }
                                                else if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "uri")==0)
                                                {
                                                    profile->Uri(CAST_TO_CHAR xmlNodeGetContent(cpp));
                                                }
                                            }
                                            cpp=cpp->next;
                                        }
                                        config->_profileList->push_back(profile);
                                    }

                                }
                                cp=cp->next;
                            }

                        }
                    }
                    pp=pp->next;
                }

                _content->push_back(config);
            }
        }
        p=p->next;
    }
}
void FtpProfileXmlParser::printAllContent()
{
    std::cout<<"FtpProfileList"<<std::endl;
    for(vector<PtrConfig>::const_iterator it=_content -> cbegin(); it!=_content->cend(); ++it)
    {
        std::cout<<"\tHost "<< (*it)->_ftpInfo->Host() <<std::endl;
        std::cout<<"\tPort "<<(*it)->_ftpInfo->Port() <<std::endl;
        std::cout<<"\tUser "<< (*it)->_ftpInfo->Usr() <<std::endl;
        std::cout<<"\tPwd "<< (*it)->_ftpInfo->Pwd() <<std::endl;
        std::cout<<"\tProfile"<<std::endl;
        for(vector<Profile *>::const_iterator itt=(*it)->_profileList->cbegin(); itt!=(*it)->_profileList->cend(); ++itt)
        {
            std::cout<<"\t\tWarehouse "<<(*itt)->Warehouse()<<std::endl;
            std::cout<<"\t\tOwner "<<(*itt)->Owner()<<std::endl;
            std::cout<<"\t\tProfileName "<<(*itt)->ProfileName()<<std::endl;
            std::cout<<"\t\tUri "<<(*itt)->Uri()<<std::endl;
            std::cout<<"\t\tDataDirection "<<(*itt)->DataDiretion()<<std::endl;
            std::cout<<"\t\tOrderType "<<(*itt)->OrderType()<<std::endl;
        }
        std::cout<<"\tProfile"<<std::endl;

    }
    std::cout<<"FtpProfileList"<<std::endl;
}
